<?php
/*
 * 银行信息
 */
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Banks as BanksModel;
use think\facade\Db;

class Banks extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
    }

    public function lists()
    {
        $param = $this->request->param();
        if (!empty($param['title'])) {
            $where = function ($query) use ($param){$query->where('name|code','LIKE', '%'.trim($param['title']).'%');};
        }else{
            $where = 1;
        }
        $list = BanksModel::where($where)->paginate();
        return $this->fetch('index', ['list' => $list]);
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证规则
            $verify = input('_verify', true);
            if($verify!='0'){
                try{
                    $this->validate($param, 'banks');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $result = BanksModel::create($param);
            !$result && $this->error($this->errorMsg);
            insert_admin_log('添加了银行信息');
            $this->success('添加成功');
        }
        return $this->fetch('save');
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $param  = $this->request->param();
            //验证规则
            $verify = input('_verify', true);
            if($verify!='0'){
                try{
                    $this->validate($param, 'banks');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $result = BanksModel::update($param);
            !$result && $this->error($this->errorMsg);
            insert_admin_log('修改了银行信息');
            $this->success('修改成功');
        }
        return $this->fetch('save',['list' => Db::table('banks')->find(input('id'))]);
    }

    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            Db::table('supplier')->where('bank_id',$param['id'])->find() && $this->error('供应商正在使用该银行，无法删除');
            BanksModel::destroy($param['id']);
            insert_admin_log('删除了银行信息');
            $this->success('删除成功');
        }
    }



}